package com.wlstock.funi.request;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;

import com.wlstock.funi.runnable.OrderListener;
import com.wlstock.funi.runnable.OrderSale;
import com.wlstock.funi.runnable.ZXRealPrice;
import com.wlstock.funi.runnable.ZXRealPrice.PriceListener;

import java.util.List;

public class QuickSaile implements PriceListener, OrderListener {
	
	private String stkCode;
	private String stkName;
	private String pty;
	public QuickSellListener qsListener;
	private boolean isAutoSaile;

	public QuickSaile(String stkCode,String stkName, String pty){
		this.stkCode = stkCode;
		this.stkName = stkName;
		this.pty = pty;
		isAutoSaile = false;
	}
	
	public void saile(boolean isAutoSaile){
		this.isAutoSaile = isAutoSaile;
		ZXRealPrice stkPrice = new ZXRealPrice(ZXRealPrice.BSFLAG_SAILE, stkCode);
		stkPrice.pListener = this;
		stkPrice.request();
	}
	
	public void saileDialog(Context mContext){
		Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("闪电卖出").setMessage("是否当前价钱直接卖出"+stkName+"?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				saile(true);
			}
		}).setNegativeButton("取消", null).show();
	}

	@Override
	public void onUpdatePriceSuccess(List<String> priList) {
		if (priList!=null) {
			try {
				//卖1价钱最低，比卖1价钱更低以便快速卖出
				Float s1 = Float.valueOf(priList.get(4)) - 0.01f;
				if (isAutoSaile){
					OrderSale saile = new OrderSale(stkCode, stkName, s1, Long.valueOf(pty));
					saile.saile(this);
				}else {
					if (qsListener!=null) {
						qsListener.getSellInfo(true,stkCode,stkName,s1,pty);
						return;
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	@Override
	public void orderSuccess(int tradeType, String stkcode, String stkName, float price, long qty) {
		if (qsListener!=null) {
			qsListener.getSellInfo(true,stkCode,stkName,price,pty);
			return;
		}
	}

	@Override
	public void orderFaile(int tradeType, String stkcode, String stkName, float price, long qty, String info) {
		if (qsListener!=null) {
			qsListener.getSellInfo(false,stkCode,stkName,price,pty);
			return;
		}
	}

	public interface QuickSellListener{
		public void getSellInfo(boolean isSuccess, String stkCode, String stkName, float price, String pty);
	}
	
}
